﻿package pw.archipelag.loaderGame.panel {
	import base.btn.BtnLabel;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinBtnPanel;
	import base.btn.BtnHitEvent;
	import base.flashUtils.ProxyClass;
	import base.flashUtils.ProxyClassEvent;
	import flash.text.TextField;
	import base.tf.TextFieldUtils;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinTf;
	import flash.text.TextFormat;
	import flash.display.Sprite;
	import com.greensock.TweenMax;
	import com.greensock.easing.Linear;

	public class BtnPanel extends BtnLabel {
		
		public var bg: Bg;
		private var skin: SkinBtnPanel;
		
		public function BtnPanel(strLabel: String): void {
			super(strLabel.toUpperCase(), this.createLabel(), this.createHit());
			this.createBg();
			this.addProxyClassEventTfLabel();
			this.addEventListener(BtnHitEvent.OVER, this.onOverHandler);
			this.addEventListener(BtnHitEvent.OUT, this.onOutHandler);
			this.dispatchEvent(new BtnHitEvent(BtnHitEvent.OUT));
		}
		
		protected function createLabel(): * {
			var tfLabel: TextField = TextFieldUtils.createTextField((new SkinTf()).tFormat);
			return tfLabel;
		}
		
		protected function createHit(): Sprite {
			var hit: Sprite = new Sprite();
			hit.graphics.beginFill(0x000000, 0);
			hit.graphics.drawRect(0, 0, 1, 20);
			hit.graphics.endFill();
			return hit;
		}
		
		private function createBg(): void {
			this.bg = new Bg(2);
			this.bg.width = this.width;
			this.bg.height = this.height;
			this.addChild(this.bg);
			this.setChildIndex(this.bg, 0);
		}
		
		private function removeBg(): void {
			this.removeChild(this.bg);
			this.bg = null;
		}
		
		private function addProxyClassEventTfLabel(): void {
			this.tfLabel = new ProxyClass(this.tfLabel);
			ProxyClass(this.tfLabel).addEventListener(ProxyClassEvent.PROPERTY_CHANGE, this.onTfLabelPropChangeCall);
			ProxyClass(this.tfLabel).addEventListener(ProxyClassEvent.PROPERTY_CALL, this.onTfLabelPropChangeCall);
		}
		
		public function onTfLabelPropChangeCall(e: ProxyClassEvent): void {
			this.width = this.tfLabel.width + 2 * (this.skin ? this.skin.pointMarginTf.x : 0);
			this.height = this.tfLabel.height + 2 * (this.skin ? this.skin.pointMarginTf.y : 0);
		}
		
		private function removeProxyClassEventTfLabel(): void {
			ProxyClass(this.tfLabel).removeEventListener(ProxyClassEvent.PROPERTY_CHANGE, this.onTfLabelPropChangeCall);
			ProxyClass(this.tfLabel).removeEventListener(ProxyClassEvent.PROPERTY_CALL, this.onTfLabelPropChangeCall);
			this.tfLabel = this.tfLabel.object;
		}
		
		public function hideShow(isHideShow: uint): void {
			TweenMax.to(this, 10, {alpha: isHideShow, useFrames: true, ease: Linear.easeNone});
		}

		override public function addMouseEvents(): void {
			this.hideShow(1);
			super.addMouseEvents();
		}
		
		override public function removeMouseEvents(): void {
			this.hideShow(0);
			this.dispatchEvent(new BtnHitEvent(BtnHitEvent.OUT));
			super.removeMouseEvents();
		}
		
		private function setElementsOnOutOver(isOutOver: uint): void {
			if (this.skin) {
				var objTween: Object = {};
				var objTweenSrc: Object = [this.skin.objTweenOut, this.skin.objTweenOver][isOutOver]
				for (var i: String in objTweenSrc)
					if (i != "time") objTween[i] = objTweenSrc[i];
				TweenMax.to(this, objTweenSrc.time || 10, objTween);
			}
		}
		
		private function onOverHandler(event: BtnHitEvent): void {
			this.setElementsOnOutOver(1);
		}
		
		private function onOutHandler(event: BtnHitEvent): void {
			this.setElementsOnOutOver(0);
		}
		
		internal function draw(skin: SkinBtnPanel = null): void {
			this.skin = skin || new SkinBtnPanel();
			this.tfLabel.x = skin.pointMarginTf.x + skin.pointMarginTfWithoutChangeBg.x;
			this.tfLabel.y = skin.pointMarginTf.y + skin.pointMarginTfWithoutChangeBg.y;
			this.bg.draw(this.skin.skinBg);
			this.skin.skinTfLabel.drawToTf(this.tfLabel);
			this.dispatchEvent(new BtnHitEvent(BtnHitEvent.OUT));
		}
		
		override public function get width(): Number {
			return this.bg.width;
		}
		
		override public function get height(): Number {
			return this.bg.height;
		}
		
		override public function set width(value: Number): void {
			this.bg.width = this.hit.width = value;
		}
		
		override public function set height(value: Number): void {
			this.bg.height = this.hit.height = value;
		}
		
		override public function destroy(): void {
			this.removeProxyClassEventTfLabel();
			this.removeBg();
			super.destroy();
		}
		
	}

}